<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <!-- 搜索 -->
        <el-col :span="10">
          <el-input placeholder="请输入内容"
                    v-model="queryInfo.query"
                    clearable
                    @clear="getOrdersList()">
            <el-button slot="append"
                       icon="el-icon-search"
                       @click="getOrdersList()"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="ordersList"
                style="width: 100%"
                border
                stripe="">
        <el-table-column type="index"
                         label="#">
        </el-table-column>
        <el-table-column prop="order_number"
                         label="订单编号">
        </el-table-column>
        <el-table-column prop="order_price"
                         label="订单价格">
        </el-table-column>
        <el-table-column label="是否支付">
          <template v-slot="scope">
            <el-tag type="success"
                    v-if="scope.row.order_pay === '1'">已支付</el-tag>
            <el-tag type="danger"
                    v-else>未支付</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send"
                         label="是否发货">
        </el-table-column>
        <el-table-column label="下单时间">
          <template v-slot="scope">
            {{scope.row.create_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作"
                         width="120px">
          <template>
            <el-tooltip class="item"
                        effect="dark"
                        content="修改地址"
                        placement="top">
              <el-button type="primary"
                         icon="el-icon-edit"
                         size="mini"
                         @click="showEditDialog"></el-button>
            </el-tooltip>
            <el-tooltip class="item"
                        effect="dark"
                        content="物流状态"
                        placement="top">
              <el-button type="success"
                         icon="el-icon-location"
                         size="mini"
                         @click="showProgressDialog"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 页码 -->
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="queryInfo.pagenum"
                     :page-sizes="[3, 5, 10]"
                     :page-size="queryInfo.pagesize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>

      <!-- 编辑对话框 -->
      <el-dialog title="修改地址"
                 :visible.sync="editDialogVisible"
                 width="50%"
                 @close="addressDialogClosed">
        <el-form :model="addressForm"
                 :rules="addressFormRules"
                 ref="addressFormRef"
                 label-width="100px">
          <el-form-item label="省市区/县"
                        prop="address1">
            <el-cascader :options="cityData"
                         v-model="addressForm.address1"
                         :props="{ expandTrigger: 'hover' }"
                         style="width:100%"></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址"
                        prop="address2">
            <el-input v-model="addressForm.address2"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer"
              class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary"
                     @click="editDialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 物流进度对话框 -->
      <el-dialog title="物流进度"
                 :visible.sync="progressDialogVisible"
                 width="50%">
        <el-timeline>
          <el-timeline-item v-for="(activity, index) in progressInfo"
                            :key="index"
                            :timestamp="activity.time">
            {{activity.context}}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getOrdersData, getProgressInfo } from '../../network/orders';
import cityData from './citydata';
export default {
  name: 'Order',
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      ordersList: [],
      total: 0,
      editDialogVisible: false,
      // 存放地址的表单
      addressForm: {
        address1: [],
        address2: ''
      },
      addressFormRules: {
        address1: [{ required: true, message: '请选择省市区/县', trigger: 'blur' }],
        address2: [{ required: true, message: '请填写详细地址', trigger: 'blur' }]
      },
      // 要选择的城市数据
      cityData,
      progressDialogVisible: false,
      //物流进度信息
      progressInfo: []

    }
  },
  created () {
    this.getOrdersList();
  },
  methods: {
    async getOrdersList () {
      const res = await getOrdersData(this.queryInfo);
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败');
      }
      //console.log(res);
      this.ordersList = res.data.goods;
      this.total = res.data.total;
    },

    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize;
      this.getOrdersList();
    },

    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage;
      this.getOrdersList();
    },

    //展示编辑对话框
    showEditDialog () {
      this.editDialogVisible = true;
    },

    //关闭对话框重置表单
    addressDialogClosed () {
      this.$refs.addressFormRef.resetFields();
    },

    async showProgressDialog () {
      this.progressDialogVisible = true;
      const res = await getProgressInfo();
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流进度失败');
      }
      this.progressInfo = res.data;
    }
  }

}
</script>

<style>
</style>